﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using VietPay.KooBoo.Data.Infrastructure;
using VietPay.KooBoo.Business.Models;
using VietPay.KooBoo.Business.Interfaces;
using VietPay.KooBoo.Data.Interfaces;
using VietPay.KooBoo.Data;
using VietPay.KooBoo.Data.EntityModels;
using System.Transactions;
using VietPay.KooBoo.Common;
using Newtonsoft.Json;

namespace VietPay.KooBoo.Business
{
    public class GoodService : ServiceBase<IGoodsModel>, IGoodsService
    {
        public GoodService()
            : this(new GoodsRepository())
        {
        }

        private IGoodsRepository _repository;

        public GoodService(IGoodsRepository goodRepository)
        {
            _repository = goodRepository ?? new GoodsRepository();          
        }

        protected override Type LogPrefix
        {
            get { return this.GetType(); }
        }

        public GoodsListViewModel GetGoodList(int? distributorID,int? productID, int PageSize, int CurrentPage)
        {
            try
            {
                if ((CurrentPage == 0) || (PageSize == 0))
                    return null;

                IQueryable<Good> query = _repository.GetQueryable();

                //if (distributorID.HasValue)
                //    query = query.Where(p=>JsonConvert.DeserializeObject<List<BatchGoodsTree>>(p.Tree).Last().DistributorID==distributorID);

                //if (productID.HasValue)
                //    query = query.Where(p => p.ProductID==productID);

                VMS2_DistributorEntities vmsEntity = new VMS2_DistributorEntities();
                List<Good> lstGood = vmsEntity.Goods.Where(p => p.ProductID == productID
                                    && JsonConvert.DeserializeObject<List<BatchGoodsTree>>(p.Tree).Last().DistributorID == distributorID)
                                    .OrderBy(p => p.GoodsID)
                                    .ToList();

                GoodsListViewModel model = new GoodsListViewModel();
                model.TotalItemCount = query.Count();
                if (model.TotalItemCount == 0)
                    return null;
                model.TotalPageCount = (int)Math.Ceiling((double)model.TotalItemCount / (double)PageSize);
                if (model.TotalPageCount != 1)
                    query = query.OrderBy(x => x.GoodsID).Skip((CurrentPage - 1) * PageSize).Take(PageSize);
                model.ThisPageItemCount = query.Count();

                model.GoodsList= new List<GoodsViewModel>();
                AutoMapper.Mapper.CreateMap<Good, GoodsViewModel>()
                    .ForMember(dest => dest.GoodsID, opt => opt.MapFrom(src => src.GoodsID))
                    .ForMember(dest => dest.Code, opt => opt.MapFrom(src => src.Code))
                    .ForMember(dest => dest.Serial, opt => opt.MapFrom(src => src.Serial))
                    .ForMember(dest => dest.Tree, opt => opt.MapFrom(src => src.Tree));
                model.GoodsList = AutoMapper.Mapper.Map(query.ToList(), model.GoodsList);

                return model;
            }
            catch (System.Exception e)
            {
                this.LogError("Error getting the Good list", e);
                return null;
            }
        }

        public GoodsViewModel GetSingle(Expression<Func<GoodsViewModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public void Add(GoodsViewModel vModel)
        {
            Good entity=new Good();
            _repository.Add(entity);
        }

        public void Delete(GoodsViewModel vModel)
        {
            Good news = _repository.GetAll(p => p.GoodsID == vModel.GoodsID).FirstOrDefault();
            _repository.Delete(news);
        }

        public void Update(GoodsViewModel vModel)
        {
            Good entity = _repository.GetAll(p => p.GoodsID == vModel.GoodsID).FirstOrDefault();
            _repository.Attach(entity);
        }

        public IList<GoodsViewModel> GetAll(Expression<Func<GoodsViewModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public IList<GoodsViewModel> GetAll()
        {
            throw new NotImplementedException();
        }

        public IQueryable<GoodsViewModel> Query(Expression<Func<GoodsViewModel, bool>> whereCondition)
        {
            List<GoodsViewModel> newsQuery = new List<GoodsViewModel>();
            IQueryable<Good> query = _repository.GetQueryable();
            AutoMapper.Mapper.CreateMap<Good, GoodsViewModel>()
                .ForMember(dest => dest.GoodsID, opt => opt.MapFrom(src => src.GoodsID))
                    .ForMember(dest => dest.Code, opt => opt.MapFrom(src => src.Code))
                    .ForMember(dest => dest.Serial,opt => opt.MapFrom(src => src.Serial))
                    .ForMember(dest => dest.Tree, opt => opt.MapFrom(src => src.Tree));
            newsQuery = AutoMapper.Mapper.Map(query, newsQuery);
            return newsQuery.AsQueryable().Where(whereCondition);
        }

        public long Count(Expression<Func<GoodsViewModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public long Count()
        {
            throw new NotImplementedException();
        }
    }
}
